package com.gnerv.pabu.basic.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.dto.CatalogDTO;
import com.gnerv.pabu.basic.core.dto.CatalogSaveDTO;
import com.gnerv.pabu.basic.core.dto.CatalogSearchDTO;
import com.gnerv.pabu.basic.core.dto.CatalogTreeDTO;
import com.gnerv.pabu.basic.core.service.ICatalogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * CMS-栏目表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Api(tags = "栏目管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/cms/catalog")
public class CatalogController {

    @Resource
    private ICatalogService catalogService;

    @ApiOperation(value = "保存 栏目")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveCatalog(@RequestBody CatalogSaveDTO catalogSaveDTO) {
        catalogService.saveCatalog(catalogSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "新建 栏目")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createCatalog(@RequestBody CatalogSaveDTO catalogSaveDTO) {
        catalogService.createCatalog(catalogSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "删除 栏目")
    @RequestMapping(value = "/delete/{catalogId}", method = RequestMethod.GET)
    public Result deleteByCatalogId(@PathVariable(value = "catalogId") String catalogId) {
        catalogService.deleteByCatalogId(catalogId);
        return Result.response();
    }

    @ApiOperation(value = "修改 栏目")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateCatalog(@RequestBody CatalogSaveDTO catalogSaveDTO) {
        catalogService.updateCatalog(catalogSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "修改 栏目状态")
    @RequestMapping(value = "/{catalogId}/status/{status}", method = RequestMethod.GET)
    public Result updateCatalogStatus(@PathVariable(value = "catalogId") String catalogId,
                                         @PathVariable(value = "status") Integer status) {
        catalogService.updateCatalogStatus(catalogId, status);
        return Result.response();
    }

    @ApiOperation(value = "查询栏目 根据栏目逻辑主键")
    @RequestMapping(value = "/select/{catalogId}", method = RequestMethod.GET)
    public Result selectCatalog(@PathVariable(value = "catalogId") String catalogId) {
        CatalogDTO catalog = catalogService.selectCatalog(catalogId);
        return Result.response().data("catalog", catalog);
    }

    @ApiOperation(value = "查询栏目列表 带分页 支持模糊查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listCatalog(CatalogSearchDTO catalog,
                              @RequestParam(value = "current", defaultValue = "1") Long current,
                              @RequestParam(value = "size", defaultValue = "300") Long size) {
        Page<CatalogDTO> catalogs = catalogService.listCatalog(catalog, current, size);
        return Result.response().data("catalogs", catalogs);
    }

    @ApiOperation(value = "根据 父id 查询 栏目树")
    @RequestMapping(value = "/list/tree/{parentId}", method = RequestMethod.GET)
    public Result listCatalogTree(@PathVariable(value = "parentId") String parentId) {
        List<CatalogTreeDTO> catalogTree = catalogService.listCatalogTree(parentId);
        return Result.response().data("catalogTree", catalogTree);
    }

}
